﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SGArchiveSystem7x.Controllers.AD
{
    public class ADController : BaseController
    {
        private string sControllerName = "AD";
        // GET: AD
        public ActionResult Index()
        {
            var n = this.VaildUser(sControllerName);
            if (n != null)
                return n;
            var columns = SGArchiveSystem7x.Models.CColumnsModel.Create_ADDomainColumns();
            ViewBag.Columns = columns;
            return View();
        }
        /// <summary>
        /// 获取AD信息
        /// </summary>
        /// <param name="depid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxADInfo(string ouname)
        {
            int count;
            var list = SGArchiveSystem7xDB.Linq.T_ADDomainDal.Get_ADDomain(out count,ouname,0, App_Code.CWebCommons._iPageRows);
            List<string> list1 = new List<string>(1);
            list1.Add(ouname);
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var json = new
            {
                rows = list.ToArray(),
                total = list.Count()
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult Get_ajaxADInfoPager(int num, int size)
        {
            ///分页后的起始行
            int iStart = (num - 1) * size;
            var listWhere = SGArchiveSystem7x.App_Code.CSession.Get_QueryWhere(Session);
            int count = 0;
            var adcontent = SGArchiveSystem7xDB.Linq.T_ADDomainDal.Get_ADContent();
            var list = SGArchiveSystem7xDB.Linq.T_ADDomainDal.Get_ADDomain(out count,listWhere[0], iStart, App_Code.CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        public string Get_ADInfo()
        {
            var adcontent = SGArchiveSystem7xDB.Linq.T_ADDomainDal.Get_ADContent();
            var list= SGArchiveSystem7xDB.Linq.T_ADDomainDal.GetAndAdd_ADUSER(adcontent.ADC_IP,adcontent.ADC_UNAME,adcontent.ADC_PWD,adcontent.ADC_OU);
            if (list == null)
            {
                return "0";
            }
            else
            {
                return "1";
            }
            
        }
        public string tb()
        {
            SGArchiveSystem7xDB.Linq.T_ADDomainDal.TB_USER();
            return "1";
        }
        public ActionResult ADPZ()
        {
            var adcontent = SGArchiveSystem7xDB.Linq.T_ADDomainDal.Get_ADContent();
            ViewBag.IP = adcontent.ADC_IP;
            ViewBag.UNAME = adcontent.ADC_UNAME;
            ViewBag.PWD = adcontent.ADC_PWD;
            ViewBag.OU = adcontent.ADC_OU;
            return View();
        }
        public string SaveADPZ(string ip,string username,string pwd,string ou)
        {
            SGArchiveSystem7xDB.Linq.T_ADDomainDal.Modify_ADContent(ip,username,pwd,ou);
            return "1";
        }
    }
}